<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.MatchString"
              clearable
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      ref="tableSortRef"
      v-loading="listLoading"
      :border="border"
      :data="list"
      :height="height"
      :size="lineHeight"
      :stripe="stripe"
      @selection-change="setSelectRows"
    >
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :sortable="item.sortable"
        width="auto"
      >
        <template slot-scope="scope">
          <span>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="300"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button text type="primary" @click="handleLine(scope.row)">
            所属产线
          </el-button>
          <el-button type="primary" @click="handleEdit21(scope.row)">
            查看日志
          </el-button>
          <!-- <el-button text type="primary" @click="handleEdit(row)">
            编辑
          </el-button> -->
          <el-button text type="danger" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
      <template slot="empty">
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <el-dialog
      :close-on-click-modal="false"
      :visible="dialogVisible"
      title="产线"
      width="60%"
    >
      <el-table :data="productLineData" style="width: 100%" height="300">
        <el-table-column label="设备编号" prop="LineId" />
        <el-table-column label="设备点位" prop="Location" />
      </el-table>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      title="日志信息"
      :visible.sync="dialogVisible222"
      width="30%"
      :before-close="handleClose222"
    >
      <el-timeline reverse>
        <el-timeline-item
          v-for="(item, index) in LogList"
          :key="index"
          :timestamp="index"
        >
          <p>零件编号：{{ item.PartId }}</p>
          <p>产品编号：{{ item.ProductId }}</p>
          <p>开始时间：{{ item.StartTime }}</p>
          <p>结束时间：{{ item.EndTime }}</p>
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible222 = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible222 = false">
          确 定
        </el-button>
      </span>
    </el-dialog>
    <table-edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import TableEdit from './components/TableEdit'
  import {
    EquipmentList,
    EquipmentDelete,
    GetDeviceLogById,
  } from '@/api/equipment'
  export default {
    components: {
      TableEdit,
    },
    data() {
      return {
        dialogVisible222: false,
        LogList: [],
        TrayInfoData: [],
        form: {
          Id: '',
          MaterialType: '',
          Content: '',
          ApplyProduct: '',
          Creator: '',
          CreateTime: new Date(),
          MaterialNum: '',
        },
        radio: 3,
        formLabelWidth: '100px',
        dialogVisible: false,
        dialogVisible1: false,
        tableSortRef: null,
        editRef: null,
        border: true,
        height: this.$baseTableHeight(1),
        stripe: false,
        lineHeight: 'small',
        isFullscreen: false,

        checkList: [
          '设备编号',
          '脱机状态',
          '设备状态',
          '设备类型',
          '设备名称',
          '设备型号',
          '设备型号',
          '设备IP',
          '设备端口',
          '设备厂家',
          '联系人',
          '联系方式',
          '创建者',
          '创建时间',
        ],
        columns: [
          {
            label: '设备编号',
            prop: 'DeviceId',
            sortable: true,
            disableCheck: true,
          },
          {
            label: '脱机状态',
            prop: 'OffLineStat',
          },
          {
            label: '设备状态',
            prop: 'DeviceStat',
          },
          {
            label: '设备类型',
            prop: 'Classify',
          },
          {
            label: '设备名称',
            prop: 'DeviceName',
          },
          {
            label: '设备型号',
            prop: 'Model',
          },
          {
            label: '设备IP',
            prop: 'IP',
          },
          {
            label: '设备端口',
            prop: 'Port',
          },
          {
            label: '设备厂家',
            prop: 'Factory',
          },
          {
            label: '创建者',
            prop: 'Creator',
          },
          {
            label: '创建时间',
            prop: 'CreateTime',
          },
        ],
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          MatchString: '',
        },
        title: '添加',
        productLineData: [],
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) =>
          this.checkList.includes(item.label)
        )
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleClose222() {
        this.dialogVisible222 = false
      },
      handleEdit21(row) {
        console.log('row', row)
        GetDeviceLogById({ DeviceId: row.DeviceId, Count: 100 }).then((res) => {
          console.log(res)
          this.dialogVisible222 = true
          this.LogList = res.LogList
        })
      },
      handleLine(row) {
        this.dialogVisible = true
        this.productLineData = row.ProductLine
      },
      handleSelectionChange(e) {
        this.form.ApplyProduct = e.map((item) => {
          return item.ProducationName
        })
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      submit() {
        this.form.CreateTime = this.days()
        MaterialType(this.form).then((res) => {
          console.log(res)
          this.$baseMessage('成功', 'success', 'vab-hey-message-success')
          this.dialogVisible = false
          fetchData()
        })
      },
      handleEdit(row) {
        if (row.UserId) {
          this.$refs['editRef'].showEdit(row)
        } else {
          console.log(' this.$refs this.$refs', this.$refs)
          this.$refs['editRef'].showEdit()
        }
      },

      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await EquipmentDelete({
            DeviceId: row.DeviceId,
          })
          // $baseMessage(msg, 'success', 'vab-hey-message-success')
          await this.fetchData()
        })
        return false
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleClose() {
        this.dialogVisible = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const params = {
          Index: this.getPage(this.queryForm.pageNo, this.queryForm.pageSize),
          Count: this.queryForm.pageSize,
          MatchString: this.queryForm.MatchString,
        }
        EquipmentList(params).then((res) => {
          this.list = res.DeviceInfo
          this.total = res.TotalCount
          this.listLoading = false
        })
      },
    },
  }
</script>
